<?php

namespace app\bll;

use app\validate\UserIntegralRule as ValidateUserIntegralRule;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use support\Db;

class UserIntegralRule
{

    /**
     * 查找
     *
     * @param integer $id
     * @return array
     */
    public function find(int $id): array
    {
        $data =  Db::table('frame_user_integral_rule')
            ->where('id', '=', $id)
            ->first();
        return $data === null ? [] : get_object_vars($data);
    }


    /**
     * 查询
     *
     * @param string $event_action
     * @return array
     */
    public function select(string $event_action = ''): array
    {
        return Db::table('frame_user_integral_rule')
            ->when($event_action !== '', function ($query) use ($event_action) {
                return $query->where('event_action', '=', $event_action);
            })
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 根据钩子查询
     *
     * @param string $hookident
     * @return array
     */
    public function selectByHookident(string $hookident): array
    {
        return Db::table('frame_user_integral_rule')
            ->where('hookident', '=', $hookident)
            ->get()
            ->map(function ($value) {
                return (array)$value;
            })
            ->toArray();
    }

    /**
     * 添加
     *
     * @param array $data
     * @return integer
     */
    public function add(array $data): int
    {

        $ValidateUserIntegralRule = new ValidateUserIntegralRule();
        if (!$ValidateUserIntegralRule->check($data)) {
            throw new Exception($ValidateUserIntegralRule->getSimpleError());
        }

        return Db::table('frame_user_integral_rule')
            ->insertGetId($data);
    }

    /**
     * 编辑规则
     *
     * @param integer $id
     * @param array $data
     * @return integer
     */
    public function edit(int $id, array $data): int
    {

        $ValidateUserIntegralRule = new ValidateUserIntegralRule();
        if (!$ValidateUserIntegralRule->check($data)) {
            throw new Exception($ValidateUserIntegralRule->getSimpleError());
        }

        return Db::table('frame_user_integral_rule')
            ->where('id', '=', $id)
            ->update($data);
    }


    /**
     * 删除
     *
     * @param integer $id
     * @return integer
     */
    public function delete(int $id): int
    {
        return Db::table('frame_user_integral_rule')
            ->where('id', '=', $id)
            ->delete();
    }

    /**
     * 分页查询
     *
     * @param string $name
     * @param string $ident
     * @param integer $pagesize
     * @return LengthAwarePaginator
     */
    public function paginator(
        string $title = '',
        int $pagesize = 20
    ): LengthAwarePaginator {
        return  Db::table('frame_user_integral_rule')
            ->leftJoin('frame_hook', 'frame_hook.ident', '=', 'frame_user_integral_rule.hookident')
            ->when($title !== '', function ($query) use ($title) {
                return $query->where('title', 'like', '%' . $title . '%');
            })
            ->orderBy('id', 'desc')
            ->paginate(
                $pagesize,
                ['frame_user_integral_rule.*', 'frame_hook.title as hooktitle']
            );
    }
}
